package org.web.accesscontrollist.server.config;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.validation.annotation.Validated;
import org.springframework.beans.factory.annotation.Value;

@Setter
@Getter
@Configuration
@Validated
@PropertySource(
value = {"classpath:application.properties", "classpath:database.properties"},
name = "ss",
encoding = "utf-8",
ignoreResourceNotFound = true)
@ConfigurationProperties(
prefix = "org.web.hello-world.hello-world-server.config1",
ignoreUnknownFields = false)
@NoArgsConstructor
public class Config1 {

    /**
    * not set in properties. 故代码中的初始值，就是获取的值。如果没有设置，则参考java基础类型默认值。
    */
    private int a1;

    /**
    * value set, key is org.web.access-control-list.access-control-list-server.config1.a2
    */
    private int a2 = 2;

    /**
    * 同时配置,需要引入ignoreUnknownFields，它会影响结果。
    * ignoreUnknownFields=false,启动报错。Reason: The elements [****] were left unbound. 都配置会造成混淆。
    * -----------------------------
    * ignoreUnknownFields=true, 不设置，即默认值为true,可以正常启动。
    * KEY="org.web.access-control-list.access-control-list-server.config1.a3" 优先级高于 KEY="org.web.access-control-list.access-control-list-server.config1.a3-also-exist"
    * 即ConfigurationProperties的优先级高于@Value.
    * ignoreUnknownFields 含义是，基于prefix,如果配置文件中有，但java类里面没有对应的属性变量与之对应，则报错，报“were left unbound. ”
    * -----------------------------
    * 给出规则：
    * （1）ignoreUnknownFields推荐使用false, 不进行忽略，增强代码健壮性。
    * (2)推荐 ConfigurationProperties>@Value
    * -----------------------------
    */
    //@Value(value="org.web.a3-not-exist")
    @Value(value = "${org.web.access-control-list.access-control-list-server.config2.a3-also-exist}")
    private int a3 = 3;

    /***
    * 配置文件中，含有有@Value 指定值，但没有配置ConfigurationProperties规则值，则@Value配置值生效。但是配置项前缀不要与prefix冲突。
    */
    private int a4 = 356;

    @Value(value = "${org.web.access-control-list.access-control-list-server.config1.a5-not-exist:444}")
    private int a5 = 321;

    private UserConfig userConfig;

    //对象加Value，启动报错。即使声明了配置。
    //@Value(value = "${org.web.access-control-list.access-control-list-server.aaa}")
    //private UserConfig userConfig2;

}
